package com.utils;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * @version v1.0
 * @ProjectName: studycoding
 * @ClassName: ImageUtil
 * @Description: TODO([图片处理帮助类])
 * @Author: Leo
 * @Date: 2020/6/13 9:36
 */
public class ImageUtil {

    public  static BufferedImage rotateImage(BufferedImage bufferedImage,int degree){

        if (bufferedImage == null) {
            return null;
        }
        if (degree < 0) {
            // 将负数角度，纠正为正数角度
            degree = degree + 360;
        }
        int imageWidth = bufferedImage.getWidth(null);
        int imageHeight = bufferedImage.getHeight(null);
        // 计算重新绘制图片的尺寸
       // Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), degree);
        // 获取原始图片的透明度
        int type = bufferedImage.getColorModel().getTransparency();
        BufferedImage newImage = null;
        newImage = new BufferedImage(imageWidth, imageHeight, type);
        Graphics2D graphics = newImage.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        // 平移位置
       // graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);
        // 旋转角度
        graphics.rotate(Math.toRadians(degree), imageWidth / 2, imageHeight / 2);
        // 绘图
        graphics.drawImage(bufferedImage, null, null);
        return newImage;
    }
}
